package com.dang.alex.dao.Imp;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.dang.alex.dao.OrderDAO;
import com.dang.alex.model.Order;
import com.dang.alex.util.DBConnection;
import com.mysql.jdbc.Connection;

public class OrderDAOImp implements OrderDAO {
	
	private static final String INSERT = "insert into d_order(user_id,status,order_time,order_desc,total_price,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?,?,?,?,?)";
	
	public void sava(Order order) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		try {
			conn = (Connection) DBConnection.getConnection();
			prep = conn.prepareStatement(INSERT, Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1, order.getUserId());
			prep.setInt(2, order.getStatus());
			prep.setLong(3, order.getOrderTime());
			prep.setString(4, order.getOrderDesc());
			prep.setDouble(5, order.getTotalPrice());
			prep.setString(6, order.getReceiveName());
			prep.setString(7, order.getFullAddress());
			prep.setString(8, order.getPostalCode());
			prep.setString(9, order.getMobile());
			prep.setString(10, order.getPhone());
			
			prep.executeUpdate();
			ResultSet rst = prep.getGeneratedKeys();
			rst.next();
			int id = rst.getInt(1);
			order.setId(id);
		}
		finally {
			DBConnection.close(prep, conn);
		}
	}
	
}
